const fs = require('fs');
const path = require('path');
const axios = require('axios');

function addFileFs(filePath, content) {
  let result;
  // 读文件
  const addContent = content;
  const newPath = path.resolve('/', filePath);
  const files = fs.readdirSync(newPath);
  files.forEach((dir) => {
    // 继续循环二级目录
    // eslint-disable-next-line
    if (isDir(dir, newPath)) {
      const dirPath = path.resolve(newPath, dir);
      const dirItemList = fs.readdirSync(dirPath);
      dirItemList.forEach((addFile) => {
        if (addFile === 'style.css' || addFile === 'arstyle.css') {
          const addFilePath = path.resolve(dirPath, addFile);
          const addResult = fs.appendFileSync(addFilePath, addContent);
          if (addResult) {
            result = false;
          } else {
            result = true;
          }
        } else {
          result = false;
        }
      });
    }
  });
  return result;
}

function isDir(fpath, filePath) {
  const currentPath = path.resolve(filePath, fpath);
  const tempStat = fs.lstatSync(currentPath);
  return tempStat.isDirectory();
}

function openorcloseIM(id) {
  axios({
    method: 'post',
    url: 'https://admin.seo.com.cn/CusSite/OpenorCloseIM',
    data: {
      id,
    },
  }).then((data) => {
    console.log(data);
  });
}

window.addFileFs = addFileFs;
window.openorcloseIM = openorcloseIM;
